package com.zhao.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.PageParam;
import com.itheima.bean.R;
import com.itheima.bean.Setmeal;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SetMealController {
    @Autowired
    private SetmealService ss;
    private List<Long> list;
    //查询分类id
    @GetMapping("/setmeal/list")
    public R findCotegoryId(Long categoryId, int status) {
        List<Setmeal> list = ss.findCotegoryId(categoryId, status);
        return R.success(list);
    }

     //新增套餐
    @PostMapping("/setmeal")
    public R add(@RequestBody SetmealDto setmealDto) {
        int row = ss.add(setmealDto);
        System.out.println("setmealDto========================================================================================== = " + setmealDto);
        //2. 判定
        if (row > 0) {
            return R.success("添加套餐成功！");
        } else {
            return R.error("添加套餐失败！");
        }


    }

    //分页查询套餐
    @GetMapping("/setmeal/page")
    public R findPage(PageParam pageParam) {

        try {
            IPage<SetmealDto> page = ss.findPage(pageParam);
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败");
        }
    }

    //删除套餐
    @DeleteMapping("/setmeal")
    public R delete(@RequestParam("ids") List<Long> list) {
        int row = ss.delete(list);
        if (row > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }


    }
    //修改起售 停售状态
    @PostMapping("/setmeal/status/0")
    public R updateStatus(@RequestParam("ids") List<Long> list) {

        int row = ss.updateStatus(list);

        if (row > 0) {
            return R.success("修改状态成功");
        } else {
            return R.error("修改状态失败");
        }

    }

    @PostMapping("/setmeal/status/1")
    public R updateStatus1(@RequestParam("ids") List<Long> list) {

        int row = ss.updateStatus(list);

        if (row > 0) {
            return R.success("修改状态成功");
        } else {
            return R.error("修改状态失败");
        }

    }

    //修改套餐
    @PutMapping("/setmeal")
    public R update(@RequestBody SetmealDto setmealDto){

        int row = ss.update(setmealDto);
        if(row>0){
            return R.success("修改套餐成功");
        }else {
            return R.error("修改套餐失败");
        }


    }




}
